﻿// ztech.cpp : 定义应用程序的入口点。
//

#include "stdafx.h"
#include "framework.h"
#include "XApp.h"
#include "helper/SAutoBuf.h"
#include "render-gdi/render-gdi.h"
#include "translator/translator.h"
#include "imgdecoder-gdip/imgdecoder-gdip.h"
#include "XMainWnd.h"
#include "SMBWebView.h"
#include "SSkinNewScrollBar.h"
#include "SpalshDlg.h"
#include "XNativeHost.h"

SOUI::SStringW  trans(const SOUI::SStringW &key,
                      const SOUI::SStringW &val1,
                      const SOUI::SStringW &val2,
                      const SOUI::SStringW &val3,
                      const SOUI::SStringW &val4) {
    SOUI::SStringW text = TR(key, L"");
    
    if (!val1.IsEmpty()) {
        text.Replace(L"&value&", val1);
    }
    if (!val2.IsEmpty()) {
        text.Replace(L"&value2&", val2);
    }
    
    if (!val3.IsEmpty()) {
        text.Replace(L"&value3&", val3);
    }
    if (!val4.IsEmpty()) {
        text.Replace(L"&value4&", val4);
    }
    
    return text;
}

SOUI::SStringW  trans(const std::string &key,
                      const std::string &val1,
                      const std::string &val2,
                      const std::string &val3,
                      const std::string &val4) {
                      
                      
    return trans(SOUI::SStrCpCvt::CvtA2W(key.c_str(), CP_UTF8),
                 SOUI::SStrCpCvt::CvtA2W(val1.c_str(), CP_UTF8),
                 SOUI::SStrCpCvt::CvtA2W(val2.c_str(), CP_UTF8),
                 SOUI::SStrCpCvt::CvtA2W(val3.c_str(), CP_UTF8),
                 SOUI::SStrCpCvt::CvtA2W(val4.c_str(), CP_UTF8));
                 
                 
}


SOUI::SStringW GetLanguage() {
    /*
    #define LLK_en_US       @"en-US"   //英语
    #define LLK_it_IT       @"it-IT"   //意大利语
    #define LLK_es_ES       @"es-ES"   //西班牙语
    #define LLK_pt_PT       @"pt-PT"   //葡萄牙语
    #define LLK_zh_CN       @"zh-CN"   //简体中文
    #define LLK_de_DE       @"de-DE"   //德语
    
    #define LLK_zh_TW       @"zh-TW"   //繁体中文
    #define LLK_ms_MY       @"ms-MY"   //马来西亚语
    #define LLK_in_ID       @"in-ID"   //印度尼西亚语
    #define LLK_vi_VN       @"vi-VN"   //越南语
    #define LLK_ko_KR       @"ko-KR"   //韩国
    */
    UINT nLanguage = ::GetOEMCP();
    SOUI::SStringW  langs;
    switch (nLanguage) {
        case 860:
            langs = L"pt-PT";
            break;
        case 950:
            langs = L"zh-TW";
            break;
        case 936:
            langs = L"zh-CN";
            break;
        case 20273:
            langs = L"de-DE";
            break;
        case 20284:
        case 1145:
            langs = L"es-ES";
            break;
        case 20297:
        case 1147:
            langs = L"fr-FR";
            break;
        case 20280:
            langs = L"it-IT";
            break;
        case 20285:
        case 437:
        default:
            langs = L"en-US";
            break;
    }
    
    return  langs;
}


int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
                      _In_opt_ HINSTANCE hPrevInstance,
                      _In_ LPWSTR    lpCmdLine,
                      _In_ int       nCmdShow) {
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);
    //GOOGLE_PROTOBUF_VERIFY_VERSION;
    
    CoInitialize(0);
    
    SOUI::SAutoRefPtr < SOUI::IImgDecoderFactory>   pImgDecoderFactory;
    SOUI::SAutoRefPtr < SOUI::IRenderFactory>       pRenderFactory;
    SOUI::SAutoRefPtr < SOUI::ITranslatorMgr>       pTransMgr;
    
    SOUI::IMGDECODOR_GDIP::SCreateInstance((SOUI::IObjRef **)&pImgDecoderFactory);
    SOUI::RENDER_GDI::SCreateInstanceGDI((SOUI::IObjRef **)&pRenderFactory);
    SOUI::TRANSLATOR::SCreateInstanceTrantor((SOUI::IObjRef **)&pTransMgr);
    
    pRenderFactory->SetImgDecoderFactory(pImgDecoderFactory);
    
    SOUI::SApplication *theSOUIApp = new SOUI::SApplication(pRenderFactory, hInstance);
    
    theSOUIApp->RegisterWindowClass<SOUI::SMBWebView>();
    theSOUIApp->RegisterSkinClass<SOUI::SSkinNewScrollbar>();
    
    XNativeHost *pRealWndHandler = new XNativeHost();
    theSOUIApp->SetRealWndHandler(pRealWndHandler);
    pRealWndHandler->Release();
    
    SOUI::SStringT strResDir = theSOUIApp->GetAppDir();
    SetCurrentDirectory(strResDir);
    //SOUI系统总是从appdir去查找资源
    theSOUIApp->SetAppDir(strResDir);
    
    theSOUIApp->SetTranslator(pTransMgr);
    
    
    xDB.initDB();
    
    if (!theSOUIApp->InitEnv(L"xstock\\theme\\default", L"file")) {
        delete theSOUIApp;
        theSOUIApp = nullptr;
        return false;
    }
    
    XMainWnd dlg;
    if (!dlg.LoadTheme(theSOUIApp, _T("white"))) {
        delete theSOUIApp;
        return false;
    }
    
    dlg.changeLang(GetLanguage());
    //{
    //    CSpalshDlg spalsh;
    //    spalsh.DoModal();
    //}
    
    dlg.Create(NULL,  WS_POPUPWINDOW | WS_CLIPCHILDREN, WS_EX_APPWINDOW, 0, 0, 0, 0);
    dlg.GetNative()->SendMessage(WM_INITDIALOG);
    dlg.GetNative()->ShowWindow(SW_SHOW);
    theSOUIApp->Run(dlg.m_hWnd);
    
    //google::protobuf::ShutdownProtobufLibrary();
    return 0;
}


